﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Kokomo.Mirror.CliMetadata
{
	class GuidRefConverter : TypeConverter
	{
		public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
		{
			return (destinationType == typeof(string));
		}
		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			if (!this.CanConvertTo(destinationType)) throw new ArgumentException("Cannot convert to type");

			GuidRef guidRef = (GuidRef)value;
			return guidRef.Guid.ToString();
		}
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
		{
			// Required for the data grid to allow editing
			return (sourceType == typeof(string));
		}
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if ((value == null) || (!this.CanConvertFrom(value.GetType()))) throw new ArgumentException("Cannot convert from type");

			GuidRef guidRef = new GuidRef(-1, new Guid((string)value));
			return guidRef;
		}
	}
}
